#!/bin/sh
LOG_DIR=/tmp/pre_install.log
echo ------开始配置------
echo ------开始配置------ >> $LOG_DIR
echo `date "+%y-%m-%d %H:%M:%S"` >>$LOG_DIR 2>&1

echo install hostapd and dnsmasq >>$LOG_DIR
pip install flask >>$LOG_DIR 2>&1
sudo apt update >>$LOG_DIR 2>&1
sudo apt install hostapd -y >>$LOG_DIR 2>&1
sudo systemctl stop hostapd >>$LOG_DIR 2>&1
sudo apt install dnsmasq -y >>$LOG_DIR 2>&1
sudo systemctl stop dnsmasq >>$LOG_DIR 2>&1


echo 配置hostapd.conf >>$LOG_DIR
sudo bash -c "cat > /etc/hostapd/hostapd.conf << EOF
interface=wlan0
driver=nl80211
ssid=hcpi
wpa_passphrase=12345678
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
hw_mode=b
channel=8
auth_algs=1
wpa=2
EOF"

echo 设置wlan0 IP地址 >>$LOG_DIR
sudo ip addr flush dev wlan0
sudo ip addr add 10.0.0.1/24 dev wlan0
sudo ip link set wlan0 up

sudo bash -c "cat > /etc/network/interfaces.d/wlan0 << EOF
auto wlan0
iface wlan0 inet static
    address 10.0.0.1
    netmask 255.255.255.0
EOF"


echo 设置DAEMON_CONF >>$LOG_DIR
sudo echo 'DAEMON_CONF="/etc/hostapd/hostapd.conf"'>>/etc/default/hostapd

sudo systemctl unmask hostapd>> $LOG_DIR 2>&1
sudo systemctl enable hostapd >> $LOG_DIR 2>&1


echo 配置DNS >> $LOG_DIR
sudo bash -c "cat > /etc/dnsmasq.conf << EOF
bind-interfaces
interface=wlan0
listen-address=10.0.0.1,127.0.0.1
dhcp-range=10.0.0.100,10.0.0.200,24h
EOF"

sudo systemctl enable dnsmasq >> $LOG_DIR 2>&1
sudo  wpa_cli -i wlan0 reconfigure >>$LOG_DIR 2>&1
# reconfigure wpa_cli 以防已连接wifi断开

echo -----配置结束-----
echo -----配置结束----- >>$LOG_DIR
exit
